package com.igl.talk.android.client.ide.dialogs;/*
 * @author Adam Wieczorek 
 *
 * 22/04/2014
 *
 * All rights reserved.
 */

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import com.igl.talk.android.client.R;
import com.igl.talk.android.client.application.BaseApplication;
import com.igl.talk.android.client.ide.activities.BaseServiceBindingActivity;
import com.igl.talk.android.client.ide.activities.MainViewActivity;

public enum DialogsFactory {

    EXIT_DIALOG {
        private boolean dontAskSelected = false;

        @Override
        public Dialog getDialog(final Context context) {

            final CharSequence[] items = { context.getResources().getString(R.string.dialog_exit_ask_before_text) };
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle(context.getResources().getString(R.string.dialog_exit_text));
            builder.setMultiChoiceItems(items, null,
                    new DialogInterface.OnMultiChoiceClickListener() {
                        // indexSelected contains the index of item (of which checkbox checked)
                        @Override
                        public void onClick(DialogInterface dialog, int indexSelected,
                                            boolean isChecked) {
                            dontAskSelected = isChecked;
                        }
                    })
                    // Set the action buttons
                    .setPositiveButton(R.string.dialog_exit_yes, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
                            SharedPreferences.Editor editor = preferences.edit();
                            editor.putBoolean(BaseApplication.PREF_EXIT_WITHOUT_ASKING, dontAskSelected);
                            editor.commit();
                            dialog.dismiss();
                            ((BaseServiceBindingActivity) context).exit();
                        }
                    })
                    .setNegativeButton(R.string.dialog_exit_no, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.dismiss();
                        }
                    });

            return builder.create();
        }
    };

    public abstract Dialog getDialog(final Context context);
}
